package org.example.netty.task;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * 服务端
 */
public class Client {
    public static void main(String[] args) throws InterruptedException {
        //工作组
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //启动器
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new ClientHandler());
                        }
                    });
            System.out.println("客户端启动");
            //连接服务端
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 10040).sync();
            //同步关闭
            channelFuture.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}
